package chances.epg.data.taglib;

import javax.servlet.jsp.JspException;

import chances.epg.entity.biz.BizPosition;
import chances.epg.entity.content.CategoryItem;
import chances.epg.entity.content.Channel;
import chances.epg.entity.content.Episode;
import chances.epg.entity.content.Program;
import chances.epg.entity.content.Schedule;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;


public class PlayUrlTag extends QueryTag {
	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 1714545301279970686L;
    private Object obj;
    private Object container;

	@Override
	public int doEndTag() throws JspException {
		AccessContext context = AccessContextUtils.getAccessContext(this.pageContext);
		
		String playUrl = "";
	    
	    if(obj instanceof Program){
            Program program = (Program)obj;
            playUrl = navigateUrlFactory.generatePlayUrl(context, program);
        }else if(obj instanceof Episode){
            Episode episode = (Episode)obj;
            playUrl = navigateUrlFactory.generatePlayUrl(context, episode);
        }else if(obj instanceof Channel){
            context = setContextContainer(context,container);
	        Channel channel = (Channel)obj;
	        playUrl = navigateUrlFactory.generatePlayUrl(context, channel);
	    }else if(obj instanceof Schedule){
            context = setContextContainer(context,container);
	        Schedule schedule = (Schedule)obj;
	        playUrl = navigateUrlFactory.generatePlayUrl(context, schedule);
        }
		
		this.setResult(playUrl);
		return super.doEndTag();
	}
	
	public AccessContext setContextContainer(AccessContext context,Object container){
	    if(container!=null){
    	    if(container instanceof CategoryItem){
                context.setContainerType(AccessContext.CATEGORY_CONTAINER_TYPE);
                context.setContainerCode(((CategoryItem)container).getCategoryCode());
            }
    	    if(container instanceof BizPosition){
                context.setContainerType(AccessContext.POSITION_CONTAINER_TYPE);
                context.setContainerCode(((BizPosition)container).getCode());
            }
	    }
	    return context;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

    public Object getContainer() {
        return container;
    }

    public void setContainer(Object container) {
        this.container = container;
    }
}